 package com.roadjava.rbac.manager;

import cn.hutool.json.JSONUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

 /**
  * @author zhaodaowen
  */
 @Component
 public class CacheManager {

     @Resource
     private RedisTemplate<String,Object> redisTemplate;

     public <T> void set(String key, T t, long timeout,TimeUnit timeUnit){
         redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(t),timeout, timeUnit);
     }

     public <T> T get(String key,Class<T> clazz){
         Object obj = redisTemplate.opsForValue().get(key);
         if (obj == null) {
             return null;
         }
         return JSONUtil.toBean(obj.toString(),clazz);
     }
     public void delete(String key) {
         redisTemplate.delete(key);
     }
 }
